package com.supermap.dataservice.cloud.api.user;
import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.mysql.config.DynamicDataSourceContextHolder;
import com.supermap.dataservice.util.AquaMd5;
import com.supermap.dataservice.util.JSONUtils;
import com.supermap.dataservice.util.MD5;
import com.supermap.dubbo.api.cloud.api.ApiUserService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author aqua
 * @desc
 * @date 2019年05月13 11:21
 * @email 1301584033@qq.com
 */
@Service
public class ApiUserServiceImpl implements ApiUserService
{

    @Autowired
    ApiUserMapper apiUserMapper;
    @Autowired
    AppKeyFactory appKeyFactory;

    @Override
    public JSONUtils setApiUserMenu(String dels, Integer apiuserId, String apimenuIds, String endTime) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        if(Strings.isNotBlank(dels)){
            apiUserMapper.delUserMenu(apiuserId,dels);
        }
        apiUserMapper.putApiUserMenu(apiuserId,apimenuIds,endTime);
        return new JSONUtils();
    }

    @Override
    public JSONUtils apiLogin(String loginName, String pwd) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        Map map=apiUserMapper.apiLogin(loginName,pwd);
        if(map!=null){
            return new JSONUtils().setMsg("登录成功!").setData(map);
        }
        return new JSONUtils().setMsg("登录失败!").setCode(100).setIsSuccess(false);
    }

    @Override
    public JSONUtils getToken(String appKey, String appScrect) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        String token="";
        Integer result=apiUserMapper.isAppKeyExist(appKey,appScrect);
        if(result>0){
                String str=appKey+appScrect+new Date().getTime();
                token= AquaMd5.string2MD5(str);
            return new JSONUtils().setData(token);
        }
        return new JSONUtils().setCode(100).setMsg("验证失败!请确认appKey,appScrect是否正确！");
    }



    @Override
    public JSONUtils upAuth(String loginName) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        String appkey=appKeyFactory.getAppKey(loginName);
        String appScrect=appKeyFactory.getAppScrect(loginName,appkey);
        apiUserMapper.upAuth(appkey,appScrect,loginName);
        Map map=new HashMap();
        map.put("appkey",appkey);
        map.put("appscrect",appScrect);
        return new JSONUtils().setData(map);
    }

    @Override
    public Integer apiTimeIsNotTake(String apiName,String appKey) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return apiUserMapper.apiTimeIsNotTake(apiName,appKey);
    }

    @Override
    public JSONUtils addApiUser(String loginName, String password,String userId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        String appkey="";
        String appsecret="";
        try {
            appkey=MD5.encode(UUID.randomUUID().toString());
            appsecret=MD5.encode(UUID.randomUUID().toString());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return new JSONUtils().setData(apiUserMapper.addApiUser( loginName,  password,  appkey, appsecret, apiUserMapper.getUserId(userId)));
    }

    @Override
    public JSONUtils delApiUser(Integer apiUserId,Integer del_flg) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtils().setData(apiUserMapper.delApiUser(apiUserId,del_flg));
    }

    @Override
    public JSONUtils getApiUserList(String apiLoginName) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtils().setData(apiUserMapper.getUserList(apiLoginName));
    }

}
